package cn.vihub.controller;

import cn.vihub.param.ShopUserParam;
import cn.vihub.pojo.ShopRole;
import cn.vihub.pojo.ShopUser;
import cn.vihub.service.ShopRoleService;
import cn.vihub.service.ShopUserService;
import cn.vihub.service.ShopUserToRoleService;
import cn.vihub.utils.Result;
import cn.vihub.vo.ShopUserVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("shop-user/user")
public class UserController {

    @Autowired
    private ShopUserService shopUserService;

    @Autowired
    private ShopUserToRoleService shopUserToRoleService;

    @Autowired
    private ShopRoleService shopRoleService;

    @GetMapping("list")
    public Result<IPage<ShopUser>> list(ShopUserParam shopUserParam){

        return Result.success(shopUserService.selectByPage(shopUserParam));

    }

    /**
     * 添加及编辑用户
     * @param shopUserParam
     * @return
     */
    @PostMapping("save")
    public Result<Boolean> save(@RequestBody ShopUserParam shopUserParam){

        if (shopUserService.save(shopUserParam)){
            return Result.success();
        }else {
            return Result.error();
        }

    }

    /**
     * 删除数据
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public Result<Boolean> delete(@PathVariable("id") Integer id){

       return Result.success(shopUserService.removeById(id));

    }

    /**
     * 用户绑定角色接口
     */
    @PostMapping("bindRole")
    public Result<Boolean> bindRole(@RequestBody ShopUserParam shopUserParam){
        return Result.success(shopUserToRoleService.binRole(shopUserParam));
    }

    /**
     * 通过用户id获取用户所关联的角色列表
     * @param shopUserParam
     * @return
     */
    @GetMapping("getRoleList")
    public Result<List<ShopRole>> getRoleList(ShopUserParam shopUserParam){
        return Result.success(shopRoleService.getRoleListByUserId(shopUserParam));
    }


    /**
     * 获取用户的详细信息
     */
    @GetMapping("getUser/{id}")
    public Result<ShopUserVo> getRoleList(@PathVariable("id") Integer id){
        return Result.success(shopUserService.getUser(id));
    }

    /**
     * 通过用户名称获取用户信息（提供给openFeign调用）
     * @param username
     * @return
     */
    @GetMapping("getUserByUsername/{username}")
    public ShopUser getUserByUsername(@PathVariable("username") String username){

        return shopUserService.getUserByUsername(username);

    }
}
